﻿#include "ObjectHierarchy.h"
#include "ui_ObjectHierarchy.h"

#include "LumDebug.h"
#include "ObjectSelector.h"
#include <QMenu>

#include "TextViewItemDelegate.h"

#include "PropertyEditView.h"

struct ObjectHierarchy::ObjectHierarchyData {
    QPointer<QObject> currentEditingObject;
};

ObjectHierarchy::~ObjectHierarchy() {
    delete ui;
}

ObjectHierarchy::ObjectHierarchy(QWidget *parent) : QWidget(parent) {
    ui = new Ui::ObjectHierarchy;
    ui->setupUi(this);
    d.reset(new ObjectHierarchyData);

    ObjectSelector::instance()->excludeObject(this);

    initUI();
    resize(360,640);

    connect(ObjectSelector::instance(), SIGNAL(currentChanged(QPointer<QObject>,QPointer<QObject>)),
            this, SLOT(onCurrentCapturedChanged(QPointer<QObject>,QPointer<QObject>)));
}

void ObjectHierarchy::onCurrentCapturedChanged(QPointer<QObject> current, const QPointer<QObject> previous) {
    Q_UNUSED(previous);
    if(!current) {
        return ;
    }
    editObject(current);
}

int ObjectHierarchy::searchObject(const QString &keywords, const QString &className) {
    if(keywords.trimmed().isEmpty() && className.trimmed().isEmpty()) {
        switchView(ui->objectTree);
        updateSearchResultLabel(false);
        return 0;
    }

    const auto& result = ui->searchTree->searchObject(keywords, className);
    updateSearchResultLabel(true, result);
    if(result == 0) {
        switchView(ui->objectTree);
    } else {
        switchView(ui->searchTree);
    }

    return result;
}

void ObjectHierarchy::setCurrentObject(QObject *object) {
    if(d->currentEditingObject == object) {
        return ;
    }

    d->currentEditingObject = object;
    currentObjectChanged(object);
}

void ObjectHierarchy::editObject(QPointer<QObject> object) {
    ui->objectTree->setRootObject(object);
    ui->searchTree->setRootObject(object);
}

void ObjectHierarchy::on_editSearch_textChanged(const QString &text) {
    updateSearch();
}

void ObjectHierarchy::initUI() {
    setStyleSheet(R"(
        QWidget {
            font-family: Consolas;
            font-size: 14px;
        }
    )");

    switchView(ui->objectTree);
    updateSearchResultLabel(false);
    initFilterCombobox();
    connect(ui->objectTree, &HierarchyObjectView::currentObjectChanged, this, &ObjectHierarchy::setCurrentObject);
    connect(ui->searchTree, &HierarchySearchView::currentObjectChanged, this, &ObjectHierarchy::setCurrentObject);
}

void ObjectHierarchy::initFilterCombobox() {
    const auto combox = ui->combox_typeFilter;
    combox->addItem("所有类型", "");

    combox->addItem("QToolButton", "QToolButton");
    combox->addItem("QPushButton", "QPushButton");
    combox->addItem("QRadioButton", "QRadioButton");
    combox->addItem("QCheckBox", "QCheckBox");

    combox->addItem("QLabel", "QLabel");
    combox->addItem("QLineEdit", "QLineEdit");
    combox->addItem("QTextEdit", "QTextEdit");
}

void ObjectHierarchy::switchView(QPointer<ObjectTreeView> view) {
    ui->objectTree->hide();
    ui->searchTree->hide();
    view->show();
}

void ObjectHierarchy::updateSearchResultLabel(bool visible, int count) {
    ui->pannel_searchResult->setVisible(visible);
    const auto &searchText = (count == 0) ?  "未匹配到结果" : QString("匹配到%1个结果").arg(count);
    ui->label_searchResultCount->setText(searchText);
}

void ObjectHierarchy::updateSearch() {
    const auto &keywords = ui->editSearch->text();
    const auto &class_name = ui->combox_typeFilter->currentData().toString();
    searchObject(keywords, class_name);
}

void ObjectHierarchy::on_combox_typeFilter_currentIndexChanged(int index) {
    updateSearch();
}

